﻿using System;
using System.Linq;
using System.Collections.Generic;
using SoftwareLogistics.SharepointClient.Library;
using SoftwareLogistics.SharepointClient.Library.Models;
using System.Diagnostics;
using System.Runtime.Serialization;

namespace SoftwareLogistics.SharepointClient.Models
{
    [DataContract]
    public class SPFormData
    {
        public SPFormData()
        {
            FormDefaults = new SPFormDefaults();
        }

        [DataMember] public bool IsEdit { get; set; }
        [DataMember] public string ListName { get; set; }
        [DataMember] public SPDataRow RowData { get; set; }
        [DataMember] public SPFormDefaults FormDefaults { get; set; }

        public List<SPField> GetFormFields()
        {
            var listMetaData = Common.ListMetaData[ListName];

            var formFields = from field
                               in listMetaData.Fields.Values
                             where field.ReadOnly == false && field.Hidden == false && !string.IsNullOrEmpty(field.DisplayName)
                             select field;

            if (IsEdit)
            {
                var dataRow = RowData;
                foreach (var field in formFields)
                {
                    var unescapedFieldName = Common.SPUnescape(field.Name);

                    if (dataRow.Columns.ContainsKey(unescapedFieldName))
                    {
                        field.Value = dataRow.Columns[unescapedFieldName];
                        Debug.WriteLine("Mapping existing data: {0} to {1}", unescapedFieldName, field.Value);
                    }
                    else
                    {
                        field.Value = null;
                        Debug.WriteLine("Wasn't able to find a match for: {0}", unescapedFieldName, field.Value);
                    }
                }
            }
            else
            {
                foreach (var field in formFields)
                {
                    var unescapedFieldName = Common.SPUnescape(field.Name);

                    if (FormDefaults.Defaults.ContainsKey(unescapedFieldName))
                    {
                        field.Value = FormDefaults.Defaults[unescapedFieldName];
                        Debug.WriteLine("Mapping field: {0} to start value: {1}", field.DisplayName, FormDefaults.Defaults[unescapedFieldName]);
                    }
                    else if (!string.IsNullOrEmpty(field.Default))
                    {
                        field.Value = field.Default;
                        Debug.WriteLine("Mapping field: {0} to default: {1}", field.DisplayName, field.Default);
                    }
                    else
                    {
                        field.Value = null;
                        Debug.WriteLine("Empty field for: {0} as {1}", field.DisplayName, unescapedFieldName);
                    }
                }
            }

            return formFields.ToList();
        }

        public IEnumerable<ValidationIssue> ProcessFields(IEnumerable<SPField> fields)
        {
            var problems = new List<ValidationIssue>();

            foreach (var field in fields)
            { 
                var problem = field.Validate();
                if(problem != null)
                    problems.Add(problem);
            }

            return problems;
        }
    }
}
